package windows
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import pure.engine.Despair;
	
	import spark.components.TitleWindow;
	
	public class ImageSectionLoadImpl extends TitleWindow
	{
		public function ImageSectionLoadImpl()
		{
			super();
		}
		
		private static var _instance:IFlexDisplayObject;
		public static function get instance():IFlexDisplayObject
		{
			return _instance || (_instance = new SwfSectionLoadWindow());
		}
		
		
		public function reset():void
		{
			
		}
		
		
		
		protected function onClick(e:CloseEvent):void
		{
			PopUpManager.removePopUp(this);
		}
		
		
		protected function ____onBrowse(e:MouseEvent):void
		{
			var file:File;
			
			switch(this.currentState)
			{
				case 'SWF':
				{
					file = new File();
					file.addEventListener(Event.SELECT,____onSelect);
					file.browseForOpen('选择要导入的SWF资源');
					break;
				}
					
				case 'IMAGE':
				{
					file = new File();
					file.addEventListener(Event.SELECT,____onSelect);
					file.browseForOpen('选择要导入的IMAGE资源');
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
		
		protected function ____onSelect(e:Event):void	
		{
			var file:File;
			var loader:Loader;
			var request:URLRequest;
			
			file = e.target as File();
			if(file.exists)
			{
				if(currentState == 'IMAGE')
				{
					if(file.extension!='png'&&file.extension!='jpg')
					{
						Alert.show("导入文件类型错误.",'提示');
					}
				}
				
				if(currentState == 'SWF')
				{
					if(file.extension!='swf')
					{
						Alert.show("导入文件类型错误.",'提示');
					}
				}
				
				loader   =  new Loader();
				request  =  new URLRequest(file.url);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,____onLoaded);
				loader.load(request);
			}
		}
		
		
		protected function ____onLoaded(e:Event):void
		{
			_parseResource(e.target.content);
		}
		
		
		protected function _parseResource(resource:*):void
		{
			if(resource is MovieClip)
			{
				Despair.makeSectionBySWF(
					}
		}
		
		
		protected function ____onFileURLChange(e:Event):void
		{
			
		}
		
		
	}
}